package com.appspot.modutajataja;

import java.util.Arrays;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;

public class MainFragment extends Fragment {
	private static final String TAG = "MainFragment";
	private UiLifecycleHelper uiHelper;
	private LoginButton authButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_intro, container, false);

		authButton = (LoginButton) view.findViewById(R.id.login_btn);
		authButton.setFragment(this);
		// To allow the fragment to receive the onActivityResult() call rather
		// than the activity, you can call the setFragment() method on the
		// LoginButton instance.
		authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));

		return view;
	}// onCreateView

	private void onSessionStateChange(Session session, SessionState state,
			Exception exception) {
		if (state.isOpened()) {
			Log.i(TAG, "Logged in...");
		} else if (state.isClosed()) {
			Log.i(TAG, "Logged out...");
		}

		if (state.isOpened()) {
			authButton.setVisibility(View.INVISIBLE);
		} else if (state.isClosed()) {
			authButton.setVisibility(View.VISIBLE);
		}
	} // callback �� ���� ��������, sessionState�� ���� ���� UI display ��
		// ��������.

	private Session.StatusCallback callback = new Session.StatusCallback() {
		@Override
		public void call(Session session, SessionState state,
				Exception exception) {
			onSessionStateChange(session, state, exception);
		}// The Session.StatusCallback ������!! implementation can override
			// the call() method to respond to session state changes and update
			// the UI accordingly.
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		uiHelper = new UiLifecycleHelper(getActivity(), callback);
		uiHelper.onCreate(savedInstanceState);
	}// The activity or fragment where you show authenticated functionality can
		// create
		// an instance of the UiLifecycleHelper class and pass in a
		// Session.StatusCallback listener
		// that is notified of any session state changes These methods are used
		// in creating, opening,
		// saving, and restoring an active Facebook session.

	@Override
	public void onResume() {
		super.onResume();
		// For scenarios where the main activity is launched and user
		// session is not null, the session state change notification
		// may not be triggered. Trigger it if it's open/closed.
		Session session = Session.getActiveSession();
		
		if (session != null && (session.isOpened() || session.isClosed())) {
			onSessionStateChange(session, session.getState(), null);
		}
		uiHelper.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	} // Your activity would need to handle the results by overriding the
		// onActivityResult() method.

	@Override
	public void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}
};